/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyecto;

import com.proyecto.datos.EntityPlan;
import com.proyecto.modelado.Plan;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author mario
 */
public class frmModificarPlan extends javax.swing.JInternalFrame {

    
    Plan pla;
    
    
    public void PopularTabla() {

        //POPULANDO TABLE 10

        DefaultTableModel tabla1;
        Object[] fila = new Object[2];
        try {
            String[] columName = {"ID", "Plan"};
            tabla1 = new DefaultTableModel(null, columName);
            //populamos el arraylist de la tabla
            ArrayList arry_10 = new ArrayList();
            pla = new Plan();
            EntityPlan entPla = new EntityPlan();

            arry_10 = pla.MostrarPlan();
            if (arry_10 != null) {
                for (int i = 0; i < arry_10.size(); i++) {
                    entPla = (EntityPlan) arry_10.get(i);
                    fila[0] = entPla.getId_plan();
                    fila[1] = entPla.getNombre_plan();
                    

                    tabla1.addRow(fila);
                }




                this.jtbleCatedratico.setModel(tabla1);
            } else {
                JOptionPane.showMessageDialog(rootPane, "No se recuperaron datos de la base de datos", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.toString());
        }
    }
    
    
    
     public void BloquearControles(){
         this.jtxtCodAsig.setEnabled(false);
         this.jtxtID.setEnabled(false);
        
         
     }
     
     public void Habilitarcontroles(){
         this.jtxtCodAsig.setEnabled(true);
         this.jtxtID.setEnabled(true);
         
         
     }
    
    
    public frmModificarPlan() {
        initComponents();
        PopularTabla();
        BloquearControles();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbtnCargar = new javax.swing.JButton();
        jbtnCancelar = new javax.swing.JButton();
        jbtnActualizar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbleCatedratico = new javax.swing.JTable();
        jtxtCodAsig = new javax.swing.JTextField();
        jtxtID = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jbtnEliminar = new javax.swing.JButton();

        jbtnCargar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/cargar.png"))); // NOI18N
        jbtnCargar.setText("Cargar");
        jbtnCargar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnCargarMouseClicked(evt);
            }
        });
        jbtnCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCargarActionPerformed(evt);
            }
        });

        jbtnCancelar.setText("Cancelar");
        jbtnCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnCancelarMouseClicked(evt);
            }
        });
        jbtnCancelar.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jbtnCancelarItemStateChanged(evt);
            }
        });

        jbtnActualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/editar.png"))); // NOI18N
        jbtnActualizar.setText("Actualizar");
        jbtnActualizar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnActualizarMouseClicked(evt);
            }
        });

        jtbleCatedratico.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jtbleCatedratico.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jScrollPane1.setViewportView(jtbleCatedratico);

        jtxtCodAsig.setText("Plan");

        jtxtID.setText("ID");

        jLabel1.setText("ID:");

        jLabel2.setText("Plan:");

        jbtnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/images (2).jpg"))); // NOI18N
        jbtnEliminar.setText("Eliminar");
        jbtnEliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnEliminarMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jbtnCancelar)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 484, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(36, 36, 36)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jbtnCargar, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(31, 31, 31))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addGap(31, 31, 31)
                                    .addComponent(jtxtID, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(15, 15, 15)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jbtnActualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(37, 37, 37)
                                    .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(36, 36, 36)
                                    .addComponent(jLabel2)
                                    .addGap(18, 18, 18)
                                    .addComponent(jtxtCodAsig, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap(40, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtCodAsig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnActualizar)
                    .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnCargar))
                .addGap(31, 31, 31)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtnCancelar)
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnCargarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnCargarMouseClicked
        Habilitarcontroles();


        int fila=this.jtbleCatedratico.getSelectedRow();

        this.jtxtID.setText(String.valueOf(this.jtbleCatedratico.getValueAt(fila, 0)));
        this.jtxtCodAsig.setText(String.valueOf(this.jtbleCatedratico.getValueAt(fila, 1)));
       

    }//GEN-LAST:event_jbtnCargarMouseClicked

    private void jbtnCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCargarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jbtnCargarActionPerformed

    private void jbtnCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnCancelarMouseClicked

        this.dispose();
    }//GEN-LAST:event_jbtnCancelarMouseClicked

    private void jbtnCancelarItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jbtnCancelarItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_jbtnCancelarItemStateChanged

    private void jbtnActualizarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnActualizarMouseClicked

        try{
            int id_plan=Integer.parseInt(this.jtxtID.getText());
            String nombre=this.jtxtCodAsig.getText();
           

            pla = new Plan();
            String msj=pla.modificarPlan(id_plan, nombre);

            JOptionPane.showMessageDialog(rootPane, msj,"Confirmacion",JOptionPane.INFORMATION_MESSAGE);

            PopularTabla();

        }catch(Exception ex){
            JOptionPane.showMessageDialog(rootPane, ex.toString(),"Error",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbtnActualizarMouseClicked

    private void jbtnEliminarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnEliminarMouseClicked
        try{
            int id_plan=Integer.parseInt(this.jtxtID.getText());

            pla = new Plan();
            String msj=pla.EliminarPlan(id_plan);

            JOptionPane.showMessageDialog(rootPane, msj,"Confirmacion",JOptionPane.INFORMATION_MESSAGE);

            PopularTabla();

        }catch(Exception ex){
            JOptionPane.showMessageDialog(rootPane, ex.toString(),"Error",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbtnEliminarMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnActualizar;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JButton jbtnCargar;
    private javax.swing.JButton jbtnEliminar;
    private javax.swing.JTable jtbleCatedratico;
    private javax.swing.JTextField jtxtCodAsig;
    private javax.swing.JTextField jtxtID;
    // End of variables declaration//GEN-END:variables
}
